<template>
  <common-detail-list title="历史记录">
    <el-button slot="header" @click="back">返回</el-button>

    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        :model="query"
        ref="queryForm"
        :inline="true"
        @submit.native.prevent
        label-width="75px"
      >
        <el-form-item label="发送时间">
          <el-date-picker
            v-model="time"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="dateChange"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="收件人邮箱" prop="toReceiver">
          <common-input
            v-model="query.toReceiver"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="发送状态">
          <el-select v-model="query.result">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="成功" value="0"></el-option>
            <el-option label="失败" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>

    <el-row slot="tools" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-switch
          v-model="isMergeOption"
          @change="mergeOptionChange"
          active-color="#13ce66"
          inactive-color="#909399"
          active-text="合并"
          inactive-text="展开"
        >
        </el-switch>
      </el-col>
    </el-row>

    <common-table slot="content" v-loading="loading" :data="list">
      <el-table-column
        label="发送时间"
        align="left"
        prop="sendTimeStr"
        width="180"
      />
      <el-table-column
        label="收件人邮箱"
        align="left"
        prop="toReceiver"
        show-overflow-tooltip
      />

      <el-table-column label="发送状态" align="left" prop="result" width="120">
        <template slot-scope="{ row }">
          <el-tag v-if="row.result === '0'" type="success">成功</el-tag>
          <el-tag v-if="row.result === '1'" type="danger">失败</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="失败原因"
        align="left"
        prop="reason"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">{{ row.reason }}</template>
      </el-table-column>
    </common-table>
    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />
  </common-detail-list>
</template>

<script>
import { getHistory } from "@/api/screenConfig/subscribe/index";

export default {
  components: {},
  props: {
    jobId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      total: 0, // 总条数
      loading: false, // table 加载中
      dialogShow: false,
      list: [],
      time: [],
      query: {
        pageNum: 1,
        pageSize: 10,
        reportJobId: "",
        result: null,
        toReceiver: null,
      }, // 查询条件
      queryType: 0,
      isMergeOption: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    back() {
      this.$emit("back");
    },
    /**
     * 获取列表
     */
    getList() {
      this.query.reportJobId = this.jobId;
      this.loading = true;
      getHistory({
        ...this.query,
        queryType: this.queryType,
        sourceType: "safety_audit",
      })
        .then((res) => {
          if (res.code === 200) {
            this.list = res.rows;
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },

    mergeOptionChange() {
      this.queryType = this.isMergeOption ? 1 : 0;
      this.handleQuery();
    },

    dateChange() {
      let endTime = null,
        beginTime = null;
      if (this.time && this.time.length > 0) {
        beginTime = this.time[0];
        endTime = this.time[1];
      }
      this.query.beginTime = beginTime || null;
      this.query.endTime = endTime || null;
    },

    /**
     * 查询
     */
    handleQuery() {
      this.query.pageNum = 1;
      this.getList();
    },
    /**
     * 重置
     */
    resetQuery() {
      this.query = this.$options.data().query;
      this.time = [];
    },
  },
};
</script>
<style lang="scss" scoped></style>
